package com.maoye.ossp.biz.intent.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.entity.AdvertAssociated;
import com.maoye.ossp.biz.contract.entity.AdvertAssshops;
import com.maoye.ossp.biz.contract.service.IAdvertAssociatedService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAdvertterminate;
import com.maoye.ossp.biz.intent.entity.IntAssterminate;
import com.maoye.ossp.biz.intent.service.IIntAdvertterminateService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 对象功能:联营撤柜意向  控制器类
 * 开发公司:MOI
 * 开发人员:zhongjt
 * 创建时间:2014-02-20 14:50:38
 */
@Controller
@RequestMapping("biz/intent/intAdvertterminate")
public class IntAdvertterminateController extends BaseController {
	@Resource
	private IIntAdvertterminateService intAdvertterminateService;
	@Resource
	private IAdvertAssociatedService  contractServce;
	
	@RequestMapping(value = "list")
	public String list(IntAdvertterminate intAdvertterminate,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		QueryFilter queryFilter = getQuerFilter(request,intAdvertterminate);
		queryFilter.addFilter("areaId", intAdvertterminate.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intAdvertterminate.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intAdvertterminate.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("startDate", intAdvertterminate.getStartDate(), QueryOP.EQUAL);
		queryFilter.addFilter("endDate", intAdvertterminate.getEndDate(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierName", intAdvertterminate.getSupplierName(), QueryOP.EQUAL);
		if(intAdvertterminate.getIntentNo()!=null){
			queryFilter.addFilter("intentNo", intAdvertterminate.getIntentNo(), QueryOP.LIKE);
		}
		String createBy = null;
		if(StringUtil.isNotEmpty(intAdvertterminate.getCreateBy())){
			createBy = ContractUtils.getUserId(intAdvertterminate.getCreateBy());
		}
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		Page<IntAdvertterminate> page = (Page<IntAdvertterminate>) intAdvertterminateService.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("intAdvertterminate", intAdvertterminate);
		return resultPage("/biz/intent/intAdvertterminate/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntAdvertterminate intAdvertterminate, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAdvertterminateService.save(intAdvertterminate);
		addMessage(redirectAttributes,
				"保存'" + intAdvertterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intAdvertterminate/list?navId="+navId;
	}

	@RequestMapping(value = "submit")
	public String submit(IntAdvertterminate intAssterminate, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAdvertterminateService.submit(intAssterminate);
		addMessage(redirectAttributes,
				"提交'" + intAssterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intAdvertterminate/list?navId="+navId;
	}
	
	@RequestMapping(value = "{contractId}/add")
	public String add( @PathVariable Long contractId,Model model,HttpServletRequest request) {
		
		model = intAdvertterminateService.init(contractId, model);
	
		return resultPage("/biz/intent/intAdvertterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntAdvertterminate intAdvertterminate = intAdvertterminateService.get(id);
		AdvertAssociated  associatedContract = contractServce.get(intAdvertterminate.getContractId().toString());
		model.addAttribute("associatedContract", associatedContract);
		model.addAttribute("intAdvertterminate", intAdvertterminate);
		return resultPage("/biz/intent/intAdvertterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntAdvertterminate intAdvertterminate = intAdvertterminateService.get(id);
		Long contractId = intAdvertterminate.getContractId();
		AdvertAssociated  associatedContract = contractServce.get(contractId.toString());
		model.addAttribute("intAdvertterminate", intAdvertterminate);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intAdvertterminate/view", model, request);
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		intAdvertterminateService.remove(id);
		addMessage(redirectAttributes, "删除撤柜意向成功！");
		return "redirect:/biz/intent/intAdvertterminate/list";
	}
	/**
	 * 获取合同信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(HttpServletRequest request,HttpServletResponse response,Model model) {
		ArrayList<AdvertAssociated> list =(ArrayList<AdvertAssociated>) contractServce.getAssociatedContractList(null);
		model.addAttribute("contractList", list);
		return "/biz/intent/intAssdiscard/modal";
	}
	
	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String item = intAdvertterminateService.removeBatch(ids);
		if(!"".equals(item)){
			addMessage(redirectAttributes,"意向单号为"+item+"的撤柜意向已经进入流程审批，不能被删除！");
		}else{
			addMessage(redirectAttributes,"删除撤柜意向成功");
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/intent/intAdvertterminate/list?navId="+navId;
	}
}
